// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
}) // 使用当前云环境

// 云函数入口函数
exports.main = async (event, context) => {
    const db = cloud.database()
    const collection = db.collection('logs')
    const fileId = event.fileId;
    try {
        const fileContent = await cloud.downloadFile({
            fileID: fileId,
        });
        const content = fileContent.fileContent.toString();
        const jsonData = JSON.parse(content);
        await collection.add({
            data: jsonData
        })
        return {
            code: 0,
            msg: '解析文件成功',
            data: jsonData
        }
    } catch (e) {
        throw '解析文件错误';
    }
}